package org.example.controller;

import org.example.dao.DeviceFaxNumberMapper;
import org.example.domain.DeviceFaxNumber;
import org.example.service.JwtTokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/app/subscription")
public class FaxSubscriptionController {

    @Autowired
    private JwtTokenManager jwtTokenManager;
    @Autowired
    private DeviceFaxNumberMapper deviceFaxNumberMapper;

    @PostMapping("/fax/upload")
    public Map<String, Object> uploadfax(@RequestHeader(name = "Authorization") String authHeader,
                                         // 从请求体获取参数并校验
                                         @RequestBody Map<String, String> request) {
        try {
            String deviceId = jwtTokenManager.validateTokenAndGetDeviceId(authHeader);


            // 构建返回用户信息
            Map<String, Object> resultInfo = new HashMap<>();

            // 1. 查询是否存在该deviceId的记录
            DeviceFaxNumber existingRecord = this.deviceFaxNumberMapper.findByDeviceID(deviceId);

            if (existingRecord != null) {
                // 2. 如果存在,填充userInfo信息
                System.out.println("ID: " + existingRecord.getId());
                DeviceFaxNumber newRecord = new DeviceFaxNumber();
                newRecord.setDeviceid(deviceId);
                newRecord.setFaxnumber(request.get("faxNumber"));
                System.out.println("fcmToken: " + request.get("fcmToken"));
                newRecord.setFcmtoken(request.get("fcmToken"));
                newRecord.setReceivenoticestatus(Integer.valueOf(request.get("sendNoticeStatus")));
                newRecord.setSendnoticestatus(Integer.valueOf(request.get("receiveNoticeStatus")));
                this.deviceFaxNumberMapper.updateFaxNumberByDeviceId(newRecord);
            } else {
                // 3. 如果不存在，创建新记录，当前时间作为id
                System.out.println("Record Exists");
                DeviceFaxNumber newRecord = new DeviceFaxNumber();
                newRecord.setDeviceid(deviceId);
                long num = System.currentTimeMillis() % Integer.MAX_VALUE;
                newRecord.setId((int)num);
                newRecord.setNickname("guest_" + String.valueOf(num).substring(6));
                newRecord.setFaxnumber(request.get("faxNumber"));
                newRecord.setFcmtoken(request.get("fcmToken"));
                newRecord.setReceivenoticestatus(Integer.valueOf(request.get("sendNoticeStatus")));
                newRecord.setSendnoticestatus(Integer.valueOf(request.get("receiveNoticeStatus")));
                System.out.println("begin to insert");
                this.deviceFaxNumberMapper.insertFaxNumberByDeviceId(newRecord);
            }
            resultInfo.put("code", 200);
            resultInfo.put("message", "success");
            return resultInfo;
        } catch (Exception e) {
            throw new RuntimeException("无效的token", e);
        }
    }
}
